<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\UserRequest;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        try {
            $model = User::paginate(\request('per_page', 10));
            return $this->success($model);
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * @param UserRequest $request
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/14 17:57
     */
    public function store(UserRequest $request)
    {
        try {
            $model = User::create($request->all());
            return $this->success($model);
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try {
            $model = User::with('roles')->findOrFail($id);
            return $this->success($model);
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            $model = User::findOrFail($id);
            $model->update($request->all());
            return $this->success($model);
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try {
            $model = User::findOrFail($id);
            $model->delete();
            return $this->success('操作成功!');
        } catch (\Exception $exception) {
            return $this->failed($exception->getMessage());
        }
    }

    /**
     * 登录功能
     * @param Request $request
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/9 16:33
     */
    public function login(Request $request)
    {
        $data = $request->all();
        $user['name'] = $data['username'];
        $user['password'] = $data['password'];
        $token = \Auth::guard('api')->attempt($user);
        if ($token) {
            return $this->success(['token' => $token]);
        }
        return $this->failed('账号密码错误!');
    }

    /**
     * 退出功能
     * @return mixed
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/9 16:49
     */
    public function logout()
    {
        auth('api')->logout();
        return $this->success('退出成功!');
    }

    /**
     * 查看当前用户登录信息
     * @author Jimmy <554511322@qq.com>
     * @date 2020/4/9 16:50
     */
    public function info()
    {
        $id = auth('api')->user()->id;
        $model = User::with(['roles'])->findOrFail($id)->toArray();
        $model['roles'] = array_column($model['roles'], 'name');
        return $this->success($model);
    }
}
